package com.yanqu.road.server.gameplayer.module.player;

import com.yanqu.road.entity.extension.UserExtensionData;
import com.yanqu.road.entity.extension.UserExtensionKeyEnum;
import com.yanqu.road.logic.bussiness.player.UserExtensionBusiness;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.base.GeneralModule;
import com.yanqu.road.server.gameplayer.module.wineshop.WineShopModule;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.util.Date;
import java.util.Map;


public class ExtensionModule extends GeneralModule {
    private static Logger logger = LogManager.getLogger(WineShopModule.class.getName());

    private Map<UserExtensionKeyEnum, UserExtensionData> userExtensionDataMap;


    public ExtensionModule(GamePlayer player) {
        super(player);
    }

    @Override
    public boolean loadData() {
        this.userExtensionDataMap = UserExtensionBusiness.getUserExtensionDataMap(this.player.getUserId());
        return true;
    }

    @Override
    public boolean afterLoadData() {
        return false;
    }

    @Override
    public boolean saveData() {
        Date now = new Date();
        for (UserExtensionData value : this.userExtensionDataMap.values()) {
            if (value.isUpdateOption()) {
                if (value.isNeedInsert()) {
                    value.setCreateAt(now);
                    value.setUpdateAt(now);
                    if (UserExtensionBusiness.addUserExtensionData(value)){
                        value.setNeedInsert(false);
                    }
                } else {
                    value.setUpdateAt(now);
                    UserExtensionBusiness.updateUserExtensionData(value);
                }
            }
        }
        return false;
    }

    public UserExtensionData getUserExtensionData(UserExtensionKeyEnum keyEnum) {
        UserExtensionData userExtensionData = this.userExtensionDataMap.get(keyEnum);
        if (userExtensionData == null) {
            userExtensionData = this.initUserExtensionData(keyEnum);
            this.userExtensionDataMap.put(userExtensionData.getExtensionKey(), userExtensionData);
        }

        return userExtensionData;
    }

    public void setUserExtensionData(UserExtensionKeyEnum keyEnum, int intValue, String stringValue) {
        UserExtensionData userExtensionData = this.userExtensionDataMap.get(keyEnum);
        if (userExtensionData == null) {
            return;
        }
        userExtensionData.setIntValue(intValue);
        userExtensionData.setStringValue(stringValue);
        userExtensionData.setUpdateOption();
    }


    public UserExtensionData initUserExtensionData(UserExtensionKeyEnum keyEnum) {
        UserExtensionData userExtensionData = new UserExtensionData();
        userExtensionData.setUserId(this.player.getUserId());
        userExtensionData.setExtensionKey(keyEnum);
        userExtensionData.setNeedInsert(true);

        return userExtensionData;
    }
}
